using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using MineFlagger;
using System.Diagnostics;

namespace MineFlagger {




	public class AIAnalysisControl : System.Windows.Forms.UserControl {

		GameBoard gameBoard;
		AIConfig aiConfig;
		MinesweeperAI mineAI;
		IViewer view;


		Path simpleMinesPath;
		Path simpleSafeCellsPath;
		Path advancedSafeCellsPath;
		Path advancedMinesPath;

		int _simpleMines = -1;
		int _simpleSafeCells = -1;
		int _advancedSafeCells = -1;
		int _advancedMines = -1;
		

		Path lastPathMarked;
		


		#region WinVars

		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.CheckBox chkAutoAnalyze;
		private System.Windows.Forms.CheckBox chkAdvancedMines;
		private System.Windows.Forms.CheckBox chkAdvancedSafeCells;
		private System.Windows.Forms.Button butAnalyzeNow;
		private System.Windows.Forms.Button butShowAdvancedSafeCells;
		private System.Windows.Forms.Button butShowAdvancedMines;
		private System.Windows.Forms.Button butUncoverNow;
		private System.Windows.Forms.CheckBox chkAutoSimpleUncover;
		private System.Windows.Forms.CheckBox chkAutoOpening;
		private System.Windows.Forms.Label llabAdvancedMines;
		private System.Windows.Forms.Label labAdvancedSafeCells;
		private System.Windows.Forms.CheckBox chkSimpleSafeCells;
		private System.Windows.Forms.CheckBox chkSimpleMines;
		private System.Windows.Forms.Button butShowSimpleSafeCells;
		private System.Windows.Forms.Label labSimpleSafeCells;
		private System.Windows.Forms.Label labSimpleMines;
		private System.Windows.Forms.Button butShowSimpleMines;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#endregion


		public AIAnalysisControl() {
			InitializeComponent();
		}

		public void SetUp(GameBoard gameBoard, AIConfig aiConfig, IViewer view, Random rnd) {
			this.gameBoard = gameBoard;
			this.aiConfig = aiConfig;
			this.view = view;

			mineAI = new MinesweeperAI(gameBoard, view, aiConfig, rnd); 

			// null all
			this.SimpleSafeCells = -1;
			this.SimpleMines = -1;
			this.AdvancedSafeCells = -1;
			this.AdvancedMines = -1;
		}

		
		#region Properties for Simple/Advanced Mines/SafeCells

		public int SimpleMines {
			get { return _simpleMines; }
			set {
				if (value == -1) {
					labSimpleMines.Text = "-";
				} else {
					labSimpleMines.Text = value.ToString();
				}
				
				butShowSimpleMines.Enabled = value > 0;

				_simpleMines = value;
			}
		}

		public int SimpleSafeCells {
			get { return _simpleSafeCells; }
			set {
				if (value == -1) {
					labSimpleSafeCells.Text = "-";
				} else {
					labSimpleSafeCells.Text = value.ToString();
				}

				butShowSimpleSafeCells.Enabled = value > 0;

				_simpleSafeCells = value;
			}
		}

		public int AdvancedSafeCells {
			get { return _advancedSafeCells; }
			set {
				if (value == -1) {
					labAdvancedSafeCells.Text = "-";
				} else {
					labAdvancedSafeCells.Text = value.ToString();
				}

				butShowAdvancedSafeCells.Enabled = value > 0;

				_advancedSafeCells = value;
			}
		}

		public int AdvancedMines {
			get { return _advancedMines; }
			set {
				if (value == -1) {
					llabAdvancedMines.Text = "-";
				} else {
					llabAdvancedMines.Text = value.ToString();
				}

				butShowAdvancedMines.Enabled = value > 0;

				_advancedMines = value;
			}
		}


		#endregion

		public bool AutoAnalyze {
			get { return chkAutoAnalyze.Checked; }
			set { chkAutoAnalyze.Checked = value; }
		}
		
		public void Analyze() {

			bool gotOne = false;

			// null all
			this.SimpleSafeCells = -1;
			this.SimpleMines = -1;
			this.AdvancedSafeCells = -1;
			this.AdvancedMines = -1;

			// TODO: Utilize AI Config
			if (chkSimpleSafeCells.Checked) {
				simpleSafeCellsPath = mineAI.SimpleUncover();
				this.SimpleSafeCells = simpleSafeCellsPath.Count;
				if (simpleSafeCellsPath.Count > 0) gotOne = true;
			}

			if (!gotOne && chkSimpleMines.Checked) {
				simpleMinesPath = mineAI.SimpleAnalysis();
				this.SimpleMines = simpleMinesPath.Count;
				if (simpleMinesPath.Count > 0) gotOne = true;
			}

			if (!gotOne && chkAdvancedSafeCells.Checked) {
				advancedSafeCellsPath = mineAI.AdvancedUncover();
				this.AdvancedSafeCells = advancedSafeCellsPath.Count;
				if (advancedSafeCellsPath.Count > 0) gotOne = true;
			}

			if (!gotOne && chkAdvancedMines.Checked) {
				advancedMinesPath = mineAI.AdvancedAnalysis();
				this.AdvancedMines = advancedMinesPath.Count;
				if (advancedMinesPath.Count > 0) gotOne = true;
			}

		}

		
		private void butAnalyzeNow_Click(object sender, System.EventArgs e) {
			Analyze();
		}



		private enum ShowType { Flag, Uncover, None }

		private void showPath(Path path, ShowType showType, bool mark, Color color) {
			if (lastPathMarked != null) {
				foreach (Cell markedCell in lastPathMarked) {
					markedCell.Marked = false;
				}
			}

			if (mark) lastPathMarked = path;
			
			foreach (Cell cell in path) {
				if (mark) {
					cell.MarkColor = color;
					cell.Marked = true;
				}

                if (showType == ShowType.Flag)
                {
                    // cell.CellState = CellState.Flagged;
                    gameBoard.SetCellState(cell, CellState.Flagged);
                }
                else if (showType == ShowType.Uncover)
                {
                    bool testResult = gameBoard.Uncover(cell);
                    Debug.Assert(testResult);
                }
			}

			view.InvalidateDirtyCells();
		}


		#region Auto Gen

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.chkAdvancedMines = new System.Windows.Forms.CheckBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.chkSimpleSafeCells = new System.Windows.Forms.CheckBox();
			this.chkSimpleMines = new System.Windows.Forms.CheckBox();
			this.chkAdvancedSafeCells = new System.Windows.Forms.CheckBox();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.butAnalyzeNow = new System.Windows.Forms.Button();
			this.chkAutoAnalyze = new System.Windows.Forms.CheckBox();
			this.butShowSimpleSafeCells = new System.Windows.Forms.Button();
			this.labSimpleSafeCells = new System.Windows.Forms.Label();
			this.labSimpleMines = new System.Windows.Forms.Label();
			this.llabAdvancedMines = new System.Windows.Forms.Label();
			this.labAdvancedSafeCells = new System.Windows.Forms.Label();
			this.butShowSimpleMines = new System.Windows.Forms.Button();
			this.butShowAdvancedSafeCells = new System.Windows.Forms.Button();
			this.butShowAdvancedMines = new System.Windows.Forms.Button();
			this.butUncoverNow = new System.Windows.Forms.Button();
			this.chkAutoSimpleUncover = new System.Windows.Forms.CheckBox();
			this.chkAutoOpening = new System.Windows.Forms.CheckBox();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkAdvancedMines
			// 
			this.chkAdvancedMines.Checked = true;
			this.chkAdvancedMines.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkAdvancedMines.Location = new System.Drawing.Point(8, 87);
			this.chkAdvancedMines.Name = "chkAdvancedMines";
			this.chkAdvancedMines.Size = new System.Drawing.Size(116, 24);
			this.chkAdvancedMines.TabIndex = 24;
			this.chkAdvancedMines.Text = "Advanced mines";
			this.chkAdvancedMines.CheckedChanged += new System.EventHandler(this.checkedChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label5,
																					this.label7});
			this.groupBox2.Location = new System.Drawing.Point(0, -5);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(220, 26);
			this.groupBox2.TabIndex = 16;
			this.groupBox2.TabStop = false;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(140, 9);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(34, 13);
			this.label5.TabIndex = 4;
			this.label5.Text = "Cells";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 9);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(89, 12);
			this.label7.TabIndex = 2;
			this.label7.Text = "AI Analysis";
			// 
			// chkSimpleSafeCells
			// 
			this.chkSimpleSafeCells.Checked = true;
			this.chkSimpleSafeCells.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkSimpleSafeCells.Location = new System.Drawing.Point(8, 24);
			this.chkSimpleSafeCells.Name = "chkSimpleSafeCells";
			this.chkSimpleSafeCells.Size = new System.Drawing.Size(124, 24);
			this.chkSimpleSafeCells.TabIndex = 21;
			this.chkSimpleSafeCells.Text = "Simple safe cells";
			this.chkSimpleSafeCells.CheckedChanged += new System.EventHandler(this.checkedChanged);
			// 
			// chkSimpleMines
			// 
			this.chkSimpleMines.Checked = true;
			this.chkSimpleMines.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkSimpleMines.Location = new System.Drawing.Point(8, 45);
			this.chkSimpleMines.Name = "chkSimpleMines";
			this.chkSimpleMines.Size = new System.Drawing.Size(123, 24);
			this.chkSimpleMines.TabIndex = 22;
			this.chkSimpleMines.Text = "Simple mines";
			this.chkSimpleMines.CheckedChanged += new System.EventHandler(this.checkedChanged);
			// 
			// chkAdvancedSafeCells
			// 
			this.chkAdvancedSafeCells.Checked = true;
			this.chkAdvancedSafeCells.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkAdvancedSafeCells.Location = new System.Drawing.Point(8, 66);
			this.chkAdvancedSafeCells.Name = "chkAdvancedSafeCells";
			this.chkAdvancedSafeCells.Size = new System.Drawing.Size(125, 24);
			this.chkAdvancedSafeCells.TabIndex = 23;
			this.chkAdvancedSafeCells.Text = "Advanced safe cells";
			this.chkAdvancedSafeCells.CheckedChanged += new System.EventHandler(this.checkedChanged);
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(0, 115);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(220, 7);
			this.progressBar1.TabIndex = 44;
			// 
			// butAnalyzeNow
			// 
			this.butAnalyzeNow.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butAnalyzeNow.Location = new System.Drawing.Point(134, 128);
			this.butAnalyzeNow.Name = "butAnalyzeNow";
			this.butAnalyzeNow.Size = new System.Drawing.Size(86, 20);
			this.butAnalyzeNow.TabIndex = 43;
			this.butAnalyzeNow.Text = "Analyze Now";
			this.butAnalyzeNow.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.butAnalyzeNow.Click += new System.EventHandler(this.butAnalyzeNow_Click);
			// 
			// chkAutoAnalyze
			// 
			this.chkAutoAnalyze.Location = new System.Drawing.Point(0, 126);
			this.chkAutoAnalyze.Name = "chkAutoAnalyze";
			this.chkAutoAnalyze.TabIndex = 42;
			this.chkAutoAnalyze.Text = "Auto analyze";
			this.chkAutoAnalyze.CheckedChanged += new System.EventHandler(this.chkAutoAnalyze_CheckedChanged);
			// 
			// butShowSimpleSafeCells
			// 
			this.butShowSimpleSafeCells.Enabled = false;
			this.butShowSimpleSafeCells.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butShowSimpleSafeCells.Location = new System.Drawing.Point(173, 27);
			this.butShowSimpleSafeCells.Name = "butShowSimpleSafeCells";
			this.butShowSimpleSafeCells.Size = new System.Drawing.Size(38, 17);
			this.butShowSimpleSafeCells.TabIndex = 41;
			this.butShowSimpleSafeCells.Text = "Show";
			this.butShowSimpleSafeCells.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.butShowSimpleSafeCells.Click += new System.EventHandler(this.butShow_Click);
			// 
			// labSimpleSafeCells
			// 
			this.labSimpleSafeCells.Location = new System.Drawing.Point(140, 29);
			this.labSimpleSafeCells.Name = "labSimpleSafeCells";
			this.labSimpleSafeCells.Size = new System.Drawing.Size(32, 16);
			this.labSimpleSafeCells.TabIndex = 45;
			this.labSimpleSafeCells.Text = "-";
			// 
			// labSimpleMines
			// 
			this.labSimpleMines.Location = new System.Drawing.Point(140, 50);
			this.labSimpleMines.Name = "labSimpleMines";
			this.labSimpleMines.Size = new System.Drawing.Size(32, 16);
			this.labSimpleMines.TabIndex = 46;
			this.labSimpleMines.Text = "-";
			// 
			// llabAdvancedMines
			// 
			this.llabAdvancedMines.Location = new System.Drawing.Point(140, 92);
			this.llabAdvancedMines.Name = "llabAdvancedMines";
			this.llabAdvancedMines.Size = new System.Drawing.Size(32, 16);
			this.llabAdvancedMines.TabIndex = 48;
			this.llabAdvancedMines.Text = "-";
			// 
			// labAdvancedSafeCells
			// 
			this.labAdvancedSafeCells.Location = new System.Drawing.Point(140, 71);
			this.labAdvancedSafeCells.Name = "labAdvancedSafeCells";
			this.labAdvancedSafeCells.Size = new System.Drawing.Size(32, 16);
			this.labAdvancedSafeCells.TabIndex = 47;
			this.labAdvancedSafeCells.Text = "-";
			// 
			// butShowSimpleMines
			// 
			this.butShowSimpleMines.Enabled = false;
			this.butShowSimpleMines.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butShowSimpleMines.Location = new System.Drawing.Point(173, 48);
			this.butShowSimpleMines.Name = "butShowSimpleMines";
			this.butShowSimpleMines.Size = new System.Drawing.Size(38, 17);
			this.butShowSimpleMines.TabIndex = 49;
			this.butShowSimpleMines.Text = "Show";
			this.butShowSimpleMines.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.butShowSimpleMines.Click += new System.EventHandler(this.butShow_Click);
			// 
			// butShowAdvancedSafeCells
			// 
			this.butShowAdvancedSafeCells.Enabled = false;
			this.butShowAdvancedSafeCells.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butShowAdvancedSafeCells.Location = new System.Drawing.Point(173, 69);
			this.butShowAdvancedSafeCells.Name = "butShowAdvancedSafeCells";
			this.butShowAdvancedSafeCells.Size = new System.Drawing.Size(38, 17);
			this.butShowAdvancedSafeCells.TabIndex = 50;
			this.butShowAdvancedSafeCells.Text = "Show";
			this.butShowAdvancedSafeCells.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.butShowAdvancedSafeCells.Click += new System.EventHandler(this.butShow_Click);
			// 
			// butShowAdvancedMines
			// 
			this.butShowAdvancedMines.Enabled = false;
			this.butShowAdvancedMines.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butShowAdvancedMines.Location = new System.Drawing.Point(173, 90);
			this.butShowAdvancedMines.Name = "butShowAdvancedMines";
			this.butShowAdvancedMines.Size = new System.Drawing.Size(38, 17);
			this.butShowAdvancedMines.TabIndex = 51;
			this.butShowAdvancedMines.Text = "Show";
			this.butShowAdvancedMines.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.butShowAdvancedMines.Click += new System.EventHandler(this.butShow_Click);
			// 
			// butUncoverNow
			// 
			this.butUncoverNow.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butUncoverNow.Location = new System.Drawing.Point(134, 152);
			this.butUncoverNow.Name = "butUncoverNow";
			this.butUncoverNow.Size = new System.Drawing.Size(86, 20);
			this.butUncoverNow.TabIndex = 53;
			this.butUncoverNow.Text = "Uncover Now";
			this.butUncoverNow.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			// 
			// chkAutoSimpleUncover
			// 
			this.chkAutoSimpleUncover.Location = new System.Drawing.Point(0, 149);
			this.chkAutoSimpleUncover.Name = "chkAutoSimpleUncover";
			this.chkAutoSimpleUncover.Size = new System.Drawing.Size(127, 24);
			this.chkAutoSimpleUncover.TabIndex = 52;
			this.chkAutoSimpleUncover.Text = "Auto simple uncover";
			this.chkAutoSimpleUncover.CheckedChanged += new System.EventHandler(this.chkAutoSimpleUncover_CheckedChanged);
			// 
			// chkAutoOpening
			// 
			this.chkAutoOpening.Location = new System.Drawing.Point(0, 172);
			this.chkAutoOpening.Name = "chkAutoOpening";
			this.chkAutoOpening.Size = new System.Drawing.Size(127, 24);
			this.chkAutoOpening.TabIndex = 54;
			this.chkAutoOpening.Text = "Auto opening";
			// 
			// AIAnalysisControl
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.chkAutoOpening,
																		  this.butUncoverNow,
																		  this.chkAutoSimpleUncover,
																		  this.butShowAdvancedMines,
																		  this.butShowAdvancedSafeCells,
																		  this.butShowSimpleMines,
																		  this.llabAdvancedMines,
																		  this.labAdvancedSafeCells,
																		  this.labSimpleMines,
																		  this.labSimpleSafeCells,
																		  this.progressBar1,
																		  this.butAnalyzeNow,
																		  this.chkAutoAnalyze,
																		  this.butShowSimpleSafeCells,
																		  this.chkAdvancedMines,
																		  this.groupBox2,
																		  this.chkSimpleSafeCells,
																		  this.chkSimpleMines,
																		  this.chkAdvancedSafeCells});
			this.Name = "AIAnalysisControl";
			this.Size = new System.Drawing.Size(220, 194);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e) {
			Analyze();
		}







		#endregion

		private void checkedChanged(object sender, System.EventArgs e) {
			 
			
			//bool butEnabmed = ((CheckBox)sender).Checked;
			
			if (!((CheckBox)sender).Checked) {

				bool enabled = false;

				if (sender == chkSimpleMines) {
					butShowSimpleMines.Enabled = enabled;
					labSimpleMines.Enabled = enabled;

				} else if (sender == chkSimpleSafeCells) {
					butShowSimpleSafeCells.Enabled = enabled;
					labSimpleSafeCells.Enabled = enabled;

				} else if (sender == chkAdvancedSafeCells) {
					butShowAdvancedSafeCells.Enabled = enabled;
					labAdvancedSafeCells.Enabled = enabled;

				} else if (sender == chkAdvancedMines) {
					butShowAdvancedMines.Enabled = enabled;
					labSimpleMines.Enabled = enabled;

				}

			} else {
				bool enabled = true;

				if (sender == chkSimpleMines) {
					this.SimpleMines = SimpleMines;
					labSimpleMines.Enabled = enabled;

				} else if (sender == chkSimpleSafeCells) {
					this.SimpleSafeCells = SimpleSafeCells;
					labSimpleSafeCells.Enabled = enabled;

				} else if (sender == chkAdvancedSafeCells) {
					this.AdvancedSafeCells = AdvancedSafeCells;
					labAdvancedSafeCells.Enabled = enabled;

				} else if (sender == chkAdvancedMines) {
					this.AdvancedMines = AdvancedMines;
					labSimpleMines.Enabled = enabled;

				}
			}
		}

		
		private void chkAutoAnalyze_CheckedChanged(object sender, System.EventArgs e) {
			validateBottomSubSet();
			if (chkAutoAnalyze.Checked) Analyze();
		}

		private void chkAutoSimpleUncover_CheckedChanged(object sender, System.EventArgs e) {
			validateBottomSubSet();
		}

		private void validateBottomSubSet() {
			butUncoverNow.Enabled = !chkAutoSimpleUncover.Checked;
			butAnalyzeNow.Enabled = !chkAutoAnalyze.Checked;
		}

		private void butShow_Click(object sender, System.EventArgs e) {
			if (sender == butShowSimpleSafeCells) {
				showPath(simpleSafeCellsPath, ShowType.None, true, Color.DarkGoldenrod);

			} else if (sender == butShowSimpleMines) {
				showPath(simpleMinesPath, ShowType.Flag, true, Color.DarkGoldenrod);

			} else if (sender == butShowAdvancedSafeCells) {
				showPath(advancedSafeCellsPath, ShowType.None, true, Color.DarkGoldenrod);

			} else if (sender == butShowAdvancedMines) {
				showPath(advancedMinesPath, ShowType.Flag, true, Color.DarkGoldenrod);
			}

			Analyze();
		}







	}
}
